package com.example.yls.demoa;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyService extends Service {
    private  boolean isLive = true;
    private MyBinder myBinder;

    @Override
    public IBinder onBind(Intent intent) {
        Log.e("Myservice","onBind");
        return  myBinder;
    }

    //启动服务
    public void onCreate(){
        super.onCreate();
        Log.e("Myservice","onCreate");

        myBinder = new MyBinder();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(isLive){
                    try{
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.e("MyService","time="+System.currentTimeMillis());
                }
            }
        }).start();
    }
    //绑定服务
    public int onStartCommand(Intent intent,int flags,int startId){
        Log.e("Myservice","onStartCommand");
        return  super.onStartCommand(intent,flags,startId);
    }
    //停止服务
    public void onDestroy(){
        isLive=false;
        super.onDestroy();
        Log.e("Myservice","onDestroy");
    }

}
